<?php
namespace app\admin\service;

use function think\where;
use com\Des;
use \think\Db;
class MemberService
{
    public function index($filter)
    {
        $member = model('member');
        
        $where = [
        ];        
        $join = [

        ];
        //未审核会员
        
        //过滤删除数据
        if(!empty($filter['is_del']) and $filter['is_del']=='2'){
            $where['a.delete_time'] = ['NEQ', 0] ;
        }else{
            $where['a.delete_time'] = 0 ;
        }
         
        $field = 'a.*';

        //判断会员类型（个人、企业）
        if(!empty($filter['type']) and $filter['type']!=0){
            $where['a.type'] = $filter['type'];
        }

        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤电话
        $mobile = empty($filter['mobile']) ? '' : $filter['mobile'];
        if (!empty($mobile)) {
            $where['a.mobile'] = ['like', "%$mobile%"];
        }
        //过滤真实姓名
        $realName = empty($filter['real_name']) ? '' : $filter['real_name'];
        if (!empty($realName)) {
            $where['a.real_name'] = ['like', "%$realName%"];
        }
        //过滤身份证号
        $id_card = empty($filter['id_card']) ?  '' :$filter['id_card'];
        if (!empty($id_card)){
            $where['a.id_card'] = $id_card;
        }
        $member = $member->where($where)->join($join)->alias('a')->field($field)->order('id','DESC')->paginate(10,false,['query'=>request()->param() ]);
        return $member;
    }
    //会员审核
    public function auditIndex($filter)
    {
        $member = model('member');
    
        $where = [
            'a.user_status'=> 2,    
        ];

        $join = [
            ['__MEMBER_DETAIL__ member_detail','a.id = member_detail.mid'],
        ];
        $field = 'member_detail.*,a.*';
         
    /*
        //判断会员类型（个人、企业）
        if(!empty($filter['type']) and $filter['type']!=0){
            $where['a.type'] = $filter['type'];
        }
*/
        
        
        if(empty($filter['mobile'])){
            $member =null;
        }else {            
            $des   = new Des();
            //对消费数据代码解密
            $mobile = $des->decrypt($filter['mobile']);
            if($mobile!==false){
                $where['a.mobile'] = $mobile;
            }else {
                $where['a.mobile'] = $filter['mobile'];
            }
            $member = $member->where($where)->join($join)->alias('a')->field($field)->find();
        }
        if(!empty($member)){
            $referee = Db::name('member')->where(["mobile"=>$member['referee_mobile']])->field('real_name')->find();
        }
        
        if(!empty($referee)){
            $member['referee_name'] = $referee['real_name'];
        }
        return $member;
    }
	
	    //修改手机号
    public function phoneUpdate($filter){
        $where = [
        ];        
        $join = [
            ['st_member b', 'a.mid=b.id'],
        ];


         
        $field = 'a.*,b.id_card,b.real_name';


        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤电话
        $mobile = empty($filter['mobile']) ? '' : $filter['mobile'];
        if (!empty($mobile)) {
            $where['b.mobile'] = ['like', "%$mobile%"];
        }

        $member = db::name('member_phone')->where($where)->join($join)->alias('a')->field($field)->order('a.create_time','DESC')->paginate(10,false,['query'=>request()->param() ]);
        return $member;  

    }

    //修改手机号
    public function recommend($filter){
        $where = [
        ];
        $join = [
            ['st_member b', 'a.mid=b.id'],
        ];

        $field = '*';


        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤电话
        $mobile = empty($filter['mobile']) ? '' : $filter['mobile'];
        if (!empty($mobile)) {
            $where['b.mobile'] = ['like', "%$mobile%"];
        }

        $member = db::name('member_referee')->where($where)->join($join)->alias('a')->field($field)->order('a.create_time','DESC')->paginate(10,false,['query'=>request()->param() ]);
        return $member;

    }
    //修改银行卡
    public function bank($filter){
        $where = [
        ];
        $join = [
            ['st_member_detail b', 'a.mid=b.mid'],
        ];
        $field = 'a.*,b.account_name,b.bank_name as y_bank_name,b.bank_card as y_bank_card';
        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤电话
        $mobile = empty($filter['account_name']) ? '' : $filter['account_name'];
        if (!empty($mobile)) {
            $where['b.account_name'] = ['like', "%$mobile%"];
        }

        $member = db::name('member_bank')->where($where)->join($join)->alias('a')->field($field)->order('a.create_time','DESC')->paginate(10,false,['query'=>request()->param() ]);
        return $member;

    }
}

